# -*- coding: utf-8 -*-
# @Date   : 2020/3/12
# @File   : R64_EG_EquityFreezeFrequencySix_CF.py
# @Author : zhaochen
# -*- coding: utf-8 -*-

'''eof
name:申请企业近6个月股权冻结次数
code:R64_EG_EquityFreezeFrequencySix
tableName:
columnName:
groups:企业业务信息校验
dependencies:EG_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime


def FormatData(tableName):
    try:
        eg = EG_DSJ.get("data").get("R1103V3")[0].get(tableName)
        return eg if isinstance(eg, list) else [{}]
    except:
        return [{}]


def float_check(target):
    try:
        float(target)
        return True
    except:
        return False

# 统一转成datetime字符串，只能转年月日的字符串
def date_format(time):
    import re
    r = re.compile(r'\D+')
    try:
        date = datetime.strptime(time, '%Y.%m.%d').strftime('%Y%m%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d').strftime('%Y%m%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d').strftime('%Y%m%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d').strftime('%Y%m%d')
                except:
                    date = None
    return date


def getRule11():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        sharesFrostList = FormatData("sharesFrostList")
        sixMonthsAgo = (datetime.now() + pd.tseries.offsets.DateOffset(months=-6)).strftime('%Y%m%d')
        now = datetime.now()
        if sharesFrostList == [{}]:
            return u'缺失值'
        else:
            if len(sharesFrostList) >= 1:
                resultList = []
                for i in sharesFrostList:
                    froFrom = i.get("froFrom")
                    froTo = i.get("froTo")
                    thawDate = i.get("thawDate")
                    if thawDate not in null_type_list:
                        if (froFrom not in null_type_list and date_format(froFrom) >= sixMonthsAgo) or (date_format(thawDate) <= now):
                            resultList.append(1)
                        else:
                            resultList.append(0)
                    else:
                        if (froFrom not in null_type_list and date_format(froFrom) >= sixMonthsAgo) or (froTo not in null_type_list and date_format(froTo) >= sixMonthsAgo):
                            resultList.append(1)
                        else:
                            resultList.append(0)

                if len(resultList) > 0:
                    return sum(resultList)
                else:
                    return u'缺失值'
            else:
                return 0
    except:
        return u'缺失值'


if __name__ == "__main__":
    file_obj = open(r'../data/rating/R64_EG_EquityFreezeFrequencySix/EG_DSJ.json', 'r')
    content = file_obj.read()
    EG_DSJ = json.loads(content, strict=False)
    aa = getRule11()
    print aa

result = getRule11()

